package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.连接工厂
        ConnectionFactory f= new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        //建立连接
        Connection con = f.newConnection();
        //建立信道
        Channel c = con.createChannel();

        //2.定义队列 helloworld
        c.queueDeclare("helloworld", false, false, false, null);

        //收到消息后用来处理消息的回调对象
        DeliverCallback callback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {

                byte[] a = message.getBody();
                String msg = new String(a);
                System.out.println("收到:"+msg);

            }
        };

        //取消或者消费的回调函数
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        //3.消费/接收消息
        c.basicConsume("helloworld", true,callback,cancelCallback);

    }
}
